﻿using System.Text;

/// <summary>
/// 异或加密解密器
/// </summary>
public class XOR
{
    // 加密解密
    public static void Do(string key, byte[] buffer, int length)
    {
        byte[] keys = Encoding.ASCII.GetBytes(key);
        int keyLen = keys.Length;
        if (keyLen <= 0) { return; }

        for (int i = 0; i < length; i++)
        {
            byte temp = buffer[i];
            buffer[i] = (byte)(temp ^ keys[i % keyLen]);
        }
    }

    // 加密解密
    public static void Do(byte[] key, int keyLen, byte[] buffer, int length)
    {
        if (keyLen <= 0) { return; }
        for (int i = 0; i < length; i++)
        {
            byte temp = buffer[i];
            buffer[i] = (byte)(temp ^ key[i % keyLen]);
        }
    }
}